using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using NUnit.Framework;

namespace Universe.Threading.Test
{
    [TestFixture]
    public class TestLock_Perfomance
    {
        Lock Sync = new Lock();
        
        [Test]
        public void Test()
        {
            Run(1, delegate(string arg) {  });
            Run(1000000, delegate(string arg) { Trace.WriteLine(arg); });
        }

        void Run(int count, Action<string> reporter)
        {
            Stopwatch sw = new Stopwatch();
            long ms;
            sw.Reset();
            sw.Start();
            for(int i=0; i<count; i++)
            {
                lock(Sync)
                {
                }
            }

            ms = sw.ElapsedMilliseconds;
            reporter(string.Format("Lock via Monitor: {0} ms", ms));

            sw.Reset();
            sw.Start();
            for (int i = 0; i < count; i++)
            {
                using (Sync.Reader)
                {
                }
            }

            ms = sw.ElapsedMilliseconds;
            reporter(string.Format("Shared Lock via ReaderWriterLock: {0} ms", ms));

            sw.Reset();
            sw.Start();
            for (int i = 0; i < count; i++)
            {
                using (Sync.Writer)
                {
                }
            }

            ms = sw.ElapsedMilliseconds;
            reporter(string.Format("Exclusive Lock via ReaderWriterLock: {0} ms", ms));
        }
    }
}
